﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.ComponentModel;

namespace MSUS.Kinect.GestureEditor.ViewModels
{
	public abstract class ViewModelBase : INotifyPropertyChanged
	{
		/// <summary>
		/// Raises the PropertyChanged event, passing along the name of the specified property
		/// </summary>
		/// <example>RaisePropertyChanged(() => MyProperty)</example>
		/// <typeparam name="T"></typeparam>
		/// <param name="propertyExpression">Lambda expression returning the property to raise the event for</param>
		protected virtual void OnPropertyChanged<T>(Expression<Func<T>> propertyExpression)
		{
			if (PropertyChanged == null)
				return;

			if (propertyExpression.Body.NodeType == ExpressionType.MemberAccess)
			{
				var memberExpr = propertyExpression.Body as MemberExpression;
				string propertyName = memberExpr.Member.Name;

				PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
			}
		}
		public event PropertyChangedEventHandler PropertyChanged;
	}
}
